﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using UWP.Douban.Api;
using UWP.Douban.Repositories;

namespace UWP.Douban.Mvvm
{
    public class LoginVm:BaseVm
    {
        private string _userName;
        private string _password;
        private bool _remeberMe;

        private ConfigRepository _configRepository;

        public LoginVm()
        {

        }

        public LoginVm(ConfigRepository configRepository)
        {
            LoginCommand = new RelayCommand(Login);
            AnonymousCommand = new RelayCommand(AnonymousLogin);

            UserName = "rwecho@126.com";
            Password = "rw870415";

            _configRepository = configRepository;
        }

        public string UserName
        {
            get
            {
                return _userName;
            }
            set
            {
                if (value == _userName) return;
                _userName = value;
                OnPropertyChanged();
            }
        }
        public string Password
        {
            get
            {
                return _password;
            }
            set
            {
                if (_password == value) return;
                _password = value;
                OnPropertyChanged();
            }
        }
        public bool RemeberMe
        {
            get
            {
                return _remeberMe;
            }
            set
            {
                if (_remeberMe == value) return;
                _remeberMe = value;
                OnPropertyChanged();
            }
        }



        public ICommand LoginCommand { get; private set; }
        public ICommand AnonymousCommand { get; private set; }

        private async void Login()
        {
            var authenticate = await GroupApi.AuthGetToken(UserName, Password);

            if (authenticate != null)
            {
                authenticate.SetCommon();
                _configRepository.SetAuthenticate(authenticate);

                var page = this.Page;
            }
        }

        private void AnonymousLogin()
        {

        }
    }
}
